// SINGULARITY HTML5 GAME ENGINE
// -----------------------------
// Copyright(c) 2013 by Codenova.pl

define(['../Core'], function (Core) {
    var Textbox = function(objTextbox) {
        "use strict";
        this.BackgroundColor = objTextbox.BackgroundColor;
        this.BorderColor = objTextbox.BorderColor;
        this.BorderWidth = objTextbox.BorderWidth;
        this.Clicked = false;
        this.Id = Core.GenerateUUID();
        this.Height = objTextbox.Height;
        this.Hovered = false;
        this.InputType = 'textbox';
        this.Label = objTextbox.Label;
        this.LabelColor = objTextbox.LabelColor;
        this.LabelFont = objTextbox.LabelFont;
        this.LabelHoverWidth = 0;
        this.LabelPosition = objTextbox.LabelPosition;
        this.LabelSize = objTextbox.LabelSize;
        this.LabelWidth = 0;
        this.Width = objTextbox.Width;
        this.Value = '';
        this.X = objTextbox.X;
        this.Y = objTextbox.Y;
    };

    Textbox.prototype = {
        constructor: Textbox,

        Add: function(objHovered) {
            var textX, textY;

            if(typeof objHovered === 'undefined' && this.Clicked == false) {
                // If LabelWidth is not set, save it to the property
                if(this.LabelWidth == 0) {
                    this.LabelWidth = Core.MeasureText(this.Label, 'normal', this.LabelFont, this.LabelSize).Width;
                }

                // We have to calculate proper position for label,
                // available positions are: top, right, bottom, left
                switch(this.LabelPosition) {
                    case 'left':
                        textX = this.X - 10;
                        textY = this.Y + (this.Height - parseInt(this.LabelSize, 10)) / 2 - 1;
                        break;
                    default:
                        break;
                }

                Core.DrawRectangleWithText(this.Context, this.X, this.Y, this.Width, this.Height, this.BackgroundColor, this.BorderColor, this.BorderWidth, this.Label, textX, textY, this.LabelColor, this.LabelSize, this.LabelFont, 'right');
            } else {
                // If element's state is set as 'clicked', override
                // objHovered object
                if(this.Clicked == true) {
                    objHovered = this.Hover;
                }

                // If LabelHoverWidth is not set, save it to the property
                if(this.LabelHoverWidth == 0) {
                    this.LabelHoverWidth = Core.MeasureText(objHovered.Label, 'normal', objHovered.LabelFont, objHovered.LabelSize).Width;
                }

                // We have to calculate proper position for label,
                // available positions are: top, right, bottom, left
                switch(this.LabelPosition) {
                    case 'left':
                        textX = objHovered.X - 10;
                        textY = objHovered.Y + (objHovered.Height - parseInt(this.LabelSize, 10)) / 2 - 1;
                        break;
                    default:
                        break;
                }

                Core.DrawRectangleWithText(this.Context, objHovered.X, objHovered.Y, objHovered.Width, objHovered.Height, objHovered.BackgroundColor, objHovered.BorderColor, objHovered.BorderWidth, objHovered.Label, textX, textY, objHovered.LabelColor, objHovered.LabelSize, objHovered.LabelFont, 'right');
            }

            // Draw text
            Core.DrawText(this.Context, this.X + Core.MeasureText(this.Value, 'normal', this.LabelFont, this.LabelSize).Width + 5, textY, this.Value, this.LabelColor, this.LabelSize, this.LabelFont, 'right');
        },

        AttachCaret: function(ctx, frame) {
            var self = this;

            // Draw caret for 60 frames
            if(frame >= 0 && frame < 60) {
                Core.DrawRectangle(ctx, this.X + 5 + Core.MeasureText(this.Value, 'normal', this.LabelFont, this.LabelSize).Width, this.Y, 2, this.Height - 1, '#fff', 'transparent', 0);
                frame++;
            }

            // Hide caret for another 60 frames
            if(frame >= 60 && frame < 120) {
                Core.DrawRectangle(ctx, this.X + 5 + Core.MeasureText(this.Value, 'normal', this.LabelFont, this.LabelSize).Width, this.Y, 2, this.Height - 1, 'transparent', 'transparent', 0);
                frame++;
            }

            // Back to the beginning
            if(frame >= 120) {
                frame = 0;
            }

            // Check if element is clicked, if not, don't
            // trigger another animation frame
            if(this.Clicked) {
                requestAnimationFrame(function() {
                    Core.RedrawUIForClick(self);
                    self.AttachCaret(ctx, frame);
                });
            } else {
                // Back to the default appearance
                Core.RedrawUIForClick(self);
            }
        },

        AttachChangeEvent: function(el) {
            var self = this;

            // We attach onKeyUp event so we can copy
            // text from HTML textbox to our Canvas element
            el.addEventListener('keyup', function() {
                // Attach HTML textbox value to our textbox
                self.Value = el.value;
            });
        },

        Create: function(objTextbox) {
            "use strict";
            return new Textbox(objTextbox);
        },

        Initialize: function() {
            Core.CreateCanvasForElement(this);
        }
    };

    return Textbox.prototype.Create;
});